

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Research
 */
@WebServlet("/Research")
public class Research extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Research() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession ses = request.getSession();
		Database data = null;
		Integer USER_ID = null;
		Integer P_ADDRESS = null;
		ResultSet rs = null;
		
		if(ses.getAttribute("USER_ID")==null){
			response.sendRedirect("Login");
			return;
		} else {
			USER_ID = (Integer) ses.getAttribute("USER_ID");
		}
		ArrayList<Integer> R_ID = new ArrayList<>();
		ArrayList<String> R_NAME = new ArrayList<>();
		ArrayList<Integer> R_COST = new ArrayList<>();
		ArrayList<Double> R_FACTOR = new ArrayList<>();
		ArrayList<Integer> ID = new ArrayList<>();
		ArrayList<String> NAME = new ArrayList<>();
		ArrayList<Double> COST = new ArrayList<>();
		ArrayList<Integer> LEVEL = new ArrayList<>();
		try {
			data = new Database();
			rs = data.Select("*").From("RESEARCH_PROJECTS").GetResult();
			while(rs.next()){
				R_ID.add(rs.getInt("RESEARCH_ID"));
				R_NAME.add(rs.getString("R_NAME"));
				R_COST.add(rs.getInt("BASE_COST"));
				R_FACTOR.add(rs.getDouble("COST_FACTOR"));
			}
			rs.close();
			data= new Database();
			rs = data.Select("*").From("RESEARCH_I").Where("USER_ID="+USER_ID).GetResult();
			while(rs.next()){
				ID.add(rs.getInt("RESEARCH_ID"));
				NAME.add(rs.getString("R_NAME"));
				LEVEL.add(rs.getInt("R_LEVEL"));
				COST.add(rs.getDouble("COST"));
			}
			rs.close();
			Iterator<Integer> iter = R_ID.iterator();
			while(iter.hasNext()){
				Integer id = iter.next();
				int index = R_ID.indexOf(id);
				if(!ID.contains(id)){
					ID.add(id);
					NAME.add(R_NAME.get(index));
					LEVEL.add(0);
					COST.add(R_COST.get(index)*1.0);
				}
			}
					
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		request.setAttribute("ID", ID);
		request.setAttribute("NAME", NAME);
		request.setAttribute("LEVEL", LEVEL);
		request.setAttribute("COST", COST);
		request.getRequestDispatcher("Research.jsp").forward(request, response);
		return;
	}

	public static int get_research_cost(int ResearchID, int Level) throws SQLException{
		Database baza = new Database();
		ResultSet rs = baza.Select("*").From("RESEARCH_PROJECTS").Where("RESEARCH_ID = " + ResearchID).GetResult();
		rs.next();
		int base_cost = rs.getInt("BASE_COST");
		float cost_factor = rs.getFloat("COST_FACTOR");
		int full_cost = (int) (base_cost * Math.pow(cost_factor, Level));
		rs.close();
		baza.Close();
		return full_cost;
	}
	
	public static int get_research_level(int playerID, int ResearchID)throws SQLException{
		Database baza = new Database();
		ResultSet rs = baza.Select("*").From("PLAYER_RESEARCH").Where("RESEARCH_ID = " + ResearchID + " AND USER_ID = " + playerID).GetResult();
		int level = 0;
		if(rs.isBeforeFirst()){
			rs.next();
			level = rs.getInt("R_LEVEL");
		}
		rs.close();
		baza.Close();
		return 0;
	}
	public static void increase_research_level(int ResearchID, int PlayerID) throws SQLException{
		Database baza = new Database();
		ResultSet rs = baza.Select("*").From("PLAYER_RESEARCH").Where("USER_ID = " + PlayerID + " AND RESEARCH_ID = " + ResearchID).GetResult();
		if(rs.isBeforeFirst()){
			rs.next();
			int level = rs.getInt("R_LEVEL");
			level = level+1;
			rs.close();
			baza.Close();
			baza = new Database();
			baza.Update("PLAYER_RESEARCH").Set("R_LEVEL = " + level).Where("USER_ID = " + PlayerID + " AND RESEARCH_ID = " + ResearchID).execute();
			baza.Close();
		} else {
			rs.close();
			baza.Close();
			baza = new Database();
			baza.InsertInto("PLAYER_RESEARCH").Values(ResearchID + ", " + PlayerID + ", " + 1).execute();
			baza.Close();
		}
		return;
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession ses = request.getSession();
		Database data = null;
		Integer USER_ID = null;
		ResultSet rs = null;
		Integer P_Address = null;
		Integer Resources = null;
		if(ses.getAttribute("USER_ID")==null){
			response.sendRedirect("Login");
			return;
		} else {
			USER_ID = (Integer) ses.getAttribute("USER_ID");
		}
		if(ses.getAttribute("P_ADDRESS")==null){
			response.sendRedirect("Overview");
			return;
		} else {
			P_Address = (Integer) ses.getAttribute("P_ADDRESS");
		}
		try {
			Resources = Overview.get_current_resources(P_Address);
			int ResearchID = Integer.parseInt(request.getParameter("rid"));
			int level = get_research_level(USER_ID, ResearchID);
			int cost = get_research_cost(ResearchID, level);
			if(cost>Resources){
				response.sendRedirect("Research");
				return;
			} else {
				Resources -= cost;
				int resID = Overview.get_current_resources(P_Address);
				Overview.update_resources(Resources, resID);
				ses.setAttribute("Resources", Resources);
				increase_research_level(ResearchID, USER_ID);
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		response.sendRedirect("Research");
		return;
	}

}
